package com.hwatsang.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.hwatsang.dao.UserDao;
import com.hwatsang.model.User;
import com.hwatsang.service.SmsService;
import com.hwatsang.service.UserService;

public class UserServiceImpl implements UserService {
	
	private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
			
	private final String PASSWORD_PLACEHOLDER = "$password";
	private final String PASSWORD_SMS = 
			"尊敬的用户，您的新密码是【" + PASSWORD_PLACEHOLDER + "】, 您现在可以用此密码登录华藏网。";
	
	@Autowired
	private UserDao userDao;
	@Autowired
	private SmsService smsService;
	
	public String createNewUser(User user) {		
		if(user == null) {
			logger.warn("Can't create a null user!");
			return null;
		}
		user.setGmtCreate(new Date());		
		user.setPassword(user.getPassword());
		
		return userDao.createUser(user);		
	}

	public boolean updateUser(User user) {
		if(user == null) {
			logger.warn("Can't update a null user!");
			return false;
		}
		
		return userDao.updateUser(user);
	}

	public boolean deleteUser(String id) {
		if(StringUtils.isBlank(id)) {
			logger.warn("Can't delete a user without a id!");
			return false;
		}
		
		return userDao.deleteUserById(id);
	}

	public List<User> listAllUsers() {
		return userDao.listUsers();
	}

	public User getUser(String userId) {
		if(StringUtils.isBlank(userId)) {
			logger.warn("Can't get a user without a id!");
			return null;
		}
		
		return userDao.getUserById(userId);
	}

	public boolean updateUserToAuthenticated(String userId) {
		User user = userDao.getUserById(userId);
		if(user != null) {
			user.setSmsAuthenticated(User.SMS_AUTH);
			return userDao.updateUser(user);
		} else {
			return false;
		}
	}
	
	public String generateNewPasswordForUser(String mobilePhone) {
		User user = userDao.getUserByMobilePhone(mobilePhone);
		if(user == null) {
			return null;			
		}
		
		String newPassword = String.valueOf((int)(Math.random() * 900000 + 100000));
		user.setPassword(newPassword);
		userDao.updateUser(user);
		
		smsService.sendSms(mobilePhone, StringUtils.replace(PASSWORD_SMS, PASSWORD_PLACEHOLDER, newPassword));
				
		return newPassword;
	}

	public User getUserByMobilePhone(String mobilePhone) {
		if(StringUtils.isBlank(mobilePhone)) {
			return null;			
		} else {
			return userDao.getUserByMobilePhone(mobilePhone);
		}
	}

	public boolean updateLastLogin(String userId, Date gmtLastLogin) {
		User user = userDao.getUserById(userId);
		if(user == null) {
			return false;
		}
		
		user.setGmtLastLogin(gmtLastLogin);
		return userDao.updateUser(user);
	}
	
	@Override
	public String generateNewLoginKeyAndStore(String userId) {
		User user = userDao.getUserById(userId);
		if(user == null) {
			return null;
		}
		
		String loginKey = UUID.randomUUID().toString();
		user.setLoginKey(loginKey);
		userDao.updateUser(user);
		
		return loginKey;
	}
}
